
import chardet
import re



class DateSizeError(Exception):#日期错误
    def __init__(self, ):
        self.message = "日期格式错误请保持格式为XXXX-XX-XX"
        super().__init__(self.message)



class ClassReadFiledFindData :
    def __init__(self) -> None:
        return 
    
    def method_read_file_find_people_by_date(self,file_path,need_find_birthday):

        date_size = '(\d){4}-(\d){2}-(\d){2}'#日期格式
        need_reaturn_data = list()#//需要返回的数据  

        try :

                        #校验日期格式是否正确
            if  re.findall(date_size,need_find_birthday):
                print("通过日期校验")
            else:
                #日期格式错误
                raise DateSizeError()

             #获取文件编码格式
            file = open(file_path,"rb")
            file_format = chardet.detect(file.read())['encoding']#//获取文件的编码方式
            print("文件编码格式是",file_format)
            file.close()
            
            file = open(file_path,"r+",encoding=file_format)
            reader_file = file.read()#打开

            for foreach_file in reader_file.split("\n") :
                finding = re.search(need_find_birthday,foreach_file)#查找
                if finding !=None :
                     need_reaturn_data.append(foreach_file) #加点

            file.close #关闭
            print(need_reaturn_data)
            return need_reaturn_data

        except FileNotFoundError:
            print("未找到文件")
        
        except DateSizeError:
            print("日期格式错误!!!!!请保持格式为XXXX-XX-XX")
            
        except Exception :
            print("其它错误")



flie_path = '身份证信息.csv'#文件路径
need_find_birthday = "1980-02-14"#要查找的出生日期

instance_ClassReadFileFindData = ClassReadFiledFindData()
finder_data = instance_ClassReadFileFindData.method_read_file_find_people_by_date(flie_path,need_find_birthday)#找到的数据

need_write_file = open("WireFile.txt",'w',encoding="utf-8")
need_write_file.write(str(finder_data))
